Skip to content

MkDetailsBlock

Show source on GitHub

Pymdownx-based details box.

Example: Regular

Jinja

{{ "Different types." | MkDetailsBlock(typ="warning") }}

Python

MkDetailsBlock(content='Different types.', typ='warning')
Warning

Different types.

/// details
    type: warning

Different types.
///
<details class="warning">
<summary>Warning</summary>
<p>Different types.</p>
</details>
MkDetailsBlock
╰── MkText('Different types.')

Example: Collapsible and expandable

Jinja

{{ "MkDetailsBlocks can also be expanded." | MkDetailsBlock(expanded=True) }}

Python

MkDetailsBlock(content='MkDetailsBlocks can also be expanded.', typ='info', expanded=True)
Info

MkDetailsBlocks can also be expanded.

/// details
    type: info
    open: True

MkDetailsBlocks can also be expanded.
///
<details class="info" open>
<summary>Info</summary>
<p>MkDetailsBlocks can also be expanded.</p>
</details>
MkDetailsBlock
╰── MkText('MkDetailsBlocks can also be expanded.')

Bases: MkBlock

__init__

__init__(
    content: str | list | MkNode | None = None,
    *,
    typ: AdmonitionTypeStr = "info",
    expanded: bool | None = None,
    title: str | None = None,
    **kwargs: Any
)

Parameters:

Name Type Description Default
content str | list | MkNode | None

Admonition content

None
typ AdmonitionTypeStr

Admonition type

'info'
expanded bool | None

Whether the details block should be expanded initially

None
title str | None

Optional Admonition title

None
kwargs Any

Keyword arguments passed to parent

{}
Name Children Inherits
MkBlock
mknodes.basenodes.mkblock
PyMdown-based block.
graph TD
  94721308064640["mkdetailsblock.MkDetailsBlock"]
  94721311698224["mkblock.MkBlock"]
  94721311697232["mkcontainer.MkContainer"]
  94721308848336["mknode.MkNode"]
  94721311766592["node.Node"]
  140564252373184["builtins.object"]
  94721311698224 --> 94721308064640
  94721311697232 --> 94721311698224
  94721308848336 --> 94721311697232
  94721311766592 --> 94721308848336
  140564252373184 --> 94721311766592
/home/runner/work/mknodes/mknodes/mknodes/basenodes/mkdetailsblock/metadata.toml
[metadata]
icon = "octicon:info-16"
name = "MkDetailsBlock"

[requirements.extension."pymdownx.blocks.details"]


[examples.regular]
title = "Regular"
jinja = """
{{ "Different types." | MkDetailsBlock(typ="warning") }}
"""

[examples.collapsible]
title = "Collapsible and expandable"
jinja = """
{{ "MkDetailsBlocks can also be expanded." | MkDetailsBlock(expanded=True) }}
"""
[output.markdown]
template = """
{{ node.fence_boundary }} details{% if node.argument %}" | " ~ {{ node.argument }}{% endif %}

    type: {{ node.typ }}
{% if node.expanded %}    open: True
{% endif %}

{{ node.items | join }}
{{ node.fence_boundary }}

"""
mknodes.basenodes.mkdetailsblock.MkDetailsBlock
class MkDetailsBlock(mkblock.MkBlock):
    """Pymdownx-based details box."""

    ICON = "octicons/info-16"
    REQUIRED_EXTENSIONS = [resources.Extension("pymdownx.blocks.details")]
    STATUS = "new"

    def __init__(
        self,
        content: str | list | mk.MkNode | None = None,
        *,
        typ: datatypes.AdmonitionTypeStr = "info",
        expanded: bool | None = None,
        title: str | None = None,
        **kwargs: Any,
    ):
        """Constructor.

        Args:
            content: Admonition content
            typ: Admonition type
            expanded: Whether the details block should be expanded initially
            title: Optional Admonition title
            kwargs: Keyword arguments passed to parent
        """
        super().__init__(
            "details",
            content=content or [],
            argument=title or "",
            attributes=dict(type=typ, open=expanded),
            **kwargs,
        )

    def __repr__(self):
        if len(self.items) == 1:
            content = reprhelpers.to_str_if_textnode(self.items[0])
        else:
            content = [reprhelpers.to_str_if_textnode(i) for i in self.items]
        return reprhelpers.get_repr(
            self,
            content=content,
            typ=self.typ,
            title=self.title,
            expanded=self.expanded,
            _filter_empty=True,
        )

    @property
    def title(self) -> str:
        return self.argument

    @title.setter
    def title(self, value: str):
        self.argument = value

    @property
    def typ(self) -> datatypes.AdmonitionTypeStr:
        return self.attributes["type"]

    @typ.setter
    def typ(self, value: datatypes.AdmonitionTypeStr):
        self.attributes["type"] = value

    @property
    def expanded(self) -> bool:
        return self.attributes["open"]

    @expanded.setter
    def expanded(self, value: bool):
        self.attributes["open"] = value